import { instance } from '@/utils/request'
import { useIntervalFn } from '@vueuse/core'
import { ElMessageBox, ElNotification } from 'element-plus'
import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

export const useLogStore = defineStore('log', () => {
  const logList = ref([])

  const logListPaging = reactive({
    total: 0,
    currentPage: 1,
    pageSize: 20,
  })

  const { resume: getLogList } = useIntervalFn(
    async (params?: object) => {
      const { data } = await instance({
        url: `/dc-error-logs/v1/list`,
        method: 'get',
        params: {
          limit: logListPaging.pageSize,
          page: logListPaging.currentPage,
          ...params,
        },
      })
      const { list = [], total = 0 } = data || {}
      logList.value = list
      logListPaging.total = total
    },
    import.meta.env.VITE_INTERVAL_TIME,
    { immediateCallback: true },
  )

  const updateLog = async (data) => {
    await ElMessageBox.confirm('确认继续操作吗', '警告')
    await instance({ url: `/dc-error-logs/v1/update`, method: 'put', data })
    getLogList()
    ElNotification({ type: 'success', title: '更新通知状态成功' })
  }

  return {
    getLogList,
    logList,
    logListPaging,
    updateLog,
  }
})
